// ActionScript file.
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.FileReference;
import flash.net.FileReferenceList;

import mx.collections.ArrayCollection;


/* 用户ID */
public var userid:Number = 0;
/* goods id*/
public var goodsid:Number = 0;


// 下拉列表的绑定变量
[Bindable] public var goodslist:ArrayCollection = new ArrayCollection([{label:"请选择商品", data:0}]);
/**/
[Bindable] public var photoslist:ArrayCollection = new ArrayCollection();


// 取商品列表的程序
private var URLCustomGoodsRPC:String = 'getcustomgoods.php';
/* 取用户选择的商品下所有上传文件的程序 */
private var URLUserGoodsRPC:String = 'getusergoods.php';
/* 取商品的详细信息 */
private var URLGoodsInfoRPC:String = 'getgoodsinfo.php';
/* 删除图片 */
private var URLDelPhotoRPC:String = 'delphoto.php';


//private var fileRef:FileReference = new FileReference();
private var fileList:FileReferenceList = new FileReferenceList();
private var imgfilter:FileFilter = new FileFilter("Image", "*.png;*.jpg;*jpeg;*JPEG");

/* 进度条 */
private var mp:myprogress;

/* 文件总数 */
private var countFile:Number = 0;

/**
 * 初始化
 * 
 * 1、取属于自制成品的商品名称
 * 
 */
public function init():void{
	this.userid = Application.application.parameters["uid"];
	this.SendHttpService(this.URLCustomGoodsRPC, null, this.getCustomGoodsRPCResult, 'Custom Goods');
	fileList.addEventListener(Event.ACTIVATE, fileActivate);
    fileList.addEventListener(Event.SELECT, fileSelect);
    fileList.addEventListener(Event.DEACTIVATE, fileDeactivate);
    fileList.addEventListener(Event.CANCEL, fileCancel);
    mp = new myprogress();
    addChild(mp);
    this.mp.visible = false;
}



/**
 *  取商品列表的回调函数。
 *  返回值为 XML格式
 * 
 */
private function getCustomGoodsRPCResult(e:ResultEvent):void{
	//trace(e.result);
	//var config:XML = XML(event.result);
	//goodslist.removeAll();
	var len1:Number = 0;
	for each (var s:XML in e.result.cata){
		goodslist.addItem({label: s.name, data: s.gid});
		var str:String = s.name;
		len1 = (len1<=str.length)?str.length:len1;
	}
	//trace(len1);
	if(len1>300){len1 = 300;}
	glist.width = (len1+10)*10;//设置下拉框的宽度
}



/**
 * 下拉列表关闭时事件
 * 
 */
private function closeHandler(event:Event):void {
	//myLabel.text = "You selected: " +  ComboBox(event.target).selectedItem.label;
    //myData.text = "Data: " +  ComboBox(event.target).selectedItem.data;
    //trace(ComboBox(event.target).selectedItem.data);
    var gid:Number = ComboBox(event.target).selectedItem.data;
    this.goodsid = gid;
    if(gid == 0){
    	this.g_desc.text = "请选择商品";
    	this.photoslist.removeAll();
    }else{
    	/* 开始读取商品信息、此商品已上传的成品 */
    	getGoodsInfo(gid);
    	getGoodsPhotos(gid, this.userid);
    }
    
} 

/**
 * 取选择的商品的信息
 * 
 * 
 */
private function getGoodsInfo(goodsid:Number):void{
	//trace(goodsid);
	this.SendHttpService(this.URLGoodsInfoRPC, {gid:goodsid},this.getGoodsInfoRPCResult, 'get Goods Info');
}

/**
 * 取商品信息的回调
 * 
 * 
 */
private function getGoodsInfoRPCResult(e:ResultEvent):void{
	//trace(e.result);
	var data:XML = XML(e.result);
	this.g_desc.htmlText = "商品名称："+data.goods_name+"\n";
	this.g_desc.htmlText+= "市  场 价：￥"+data.market_price+"\n";
	this.g_desc.htmlText+= "会  员 价：￥"+data.shop_price+"\n";
	this.g_desc.htmlText+= "商品说明：\n";
	this.g_desc.htmlText+= data.goods_desc+"\n";
}

/**
 * 取此商品的图片
 * 
 */
private function getGoodsPhotos(goodsid:Number,user_id:Number):void{
	this.SendHttpService(this.URLUserGoodsRPC, {gid:goodsid,uid:user_id},this.getUserGoodsRPCResult, 'get Goods Photos');
}

/**
 * 取商品图片的回调
 * 
 */
private function getUserGoodsRPCResult(e:ResultEvent):void{
	//trace(e.result);
	this.photoslist.removeAll();
	for each (var s:XML in e.result.photo){
		photoslist.addItem({url: s.thumb, id: s.id});
	}
}


/**
 * 删除图片
 * 
 */
public function delMyMaked(pid:Object):void{
	trace(pid);
	this.SendHttpService(this.URLDelPhotoRPC, {id:pid},this.delPhotoRPCResult, 'delete photo');
}

/**
 * 删除指定照片
 */
private function delPhotoRPCResult(e:ResultEvent):void{
	trace(e.result);
	this.getGoodsPhotos(this.goodsid, this.userid);
}

/**
 * 选择照片
 * 
 */
private function SelectPhoto():void{
	if(this.goodsid!=0){
		fileList.browse([imgfilter]);
	}else{
		Alert.show('请先选择商品','注意');
	}
}

private function fileActivate(e:Event):void{

}


/**
 * 文件选择
 *
 */
private function fileSelect(e:Event):void{
	
    var request:URLRequest = new URLRequest("upload.php");
    var file:FileReference;
    var files:FileReferenceList = FileReferenceList(e.target);
    var selectedFileArray:Array = files.fileList;
    var variables:URLVariables = new URLVariables();
    variables.userid= this.userid ;
    variables.gid = this.goodsid;
    request.data=variables;
	request.method=URLRequestMethod.POST;
	this.mp.visible = true;
	this.countFile = selectedFileArray.length;
    for(var i:uint=0; i<selectedFileArray.length; i++){
    	file = FileReference(selectedFileArray[i]);
        file.addEventListener(Event.COMPLETE, completeHandler);
        file.addEventListener(IOErrorEvent.IO_ERROR, fileErrorHandle);
        file.addEventListener(ProgressEvent.PROGRESS, progressHandle);
        try{
        	file.upload(request);
        }catch (error:Error){
            trace("Unable to upload files.");
            Alert.show('上传失败','错误');
        }
    }
}

private function fileDeactivate(e:Event):void{

}

private function fileCancel(e:Event):void{

}

/**
 * FileReference的complete事件
 */
private function completeHandler(e:Event):void{
       //涓婁紶鎴愬姛
       this.getGoodsPhotos(this.goodsid, this.userid);
       this.countFile--;
       if(this.countFile==0){
       		this.mp.visible = false;
       }
}

/**
 * 文件IO错误
 */
private function fileErrorHandle(e:ErrorEvent):void{
	trace(e.text);
	Alert.show("系统错误！","错误");
}

/* 进入购物车 */
private function tocart():void{
	if(this.goodsid!=0){
		navigateToURL(new URLRequest('addtocar.php?id='+this.goodsid), '_self');
	}else{
		Alert.show('请先选择商品','注意');
	}
}

private function progressHandle(e:ProgressEvent):void{
	var prog:Number = e.bytesLoaded/e.bytesTotal;
	this.mp.goprocess(prog);
}
